﻿Public Class LocaliteSite
    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Dim idAct As String = ""
    Public databaseName As String
    Private Sub LocaliteSite_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""
        DataGridViewPop.MultiSelect = False
        DataGridViewPop.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewPop.ReadOnly = True

        majour()
    End Sub

    Private Sub majour()
        Try
            db.myReq = "SELECT * FROM  [dbo].[LocaliteSite]"
            db.Connecter(databaseName)
            Me.DataGridViewPop.Rows.Clear()
            While db.rdr.Read()
                DataGridViewPop.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6))
                idAct = db.rdr(0)
            End While
            db.Disconnecter()

            If DataGridViewPop.RowCount - 1 = 0 Then
                Me.TextBoxLong.Text = ""
                Me.TextBoxLat.Text = ""
                Me.TextBoxAlt.Text = ""
                Me.TextBoxLocl.Text = ""
            Else
                DataGridViewPop.Rows(0).Selected = True
                Me.TextBoxLong.Text = DataGridViewPop.SelectedCells(4).Value
                Me.TextBoxLat.Text = DataGridViewPop.SelectedCells(3).Value
                Me.TextBoxAlt.Text = DataGridViewPop.SelectedCells(5).Value
                Me.TextBoxLocl.Text = DataGridViewPop.SelectedCells(2).Value
                Me.oidValue.Text = DataGridViewPop.SelectedCells(6).Value
            End If

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub DataGridViewPop_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewPop.CurrentCellChanged
        If Entrer = "debut" Then
            selection()
        End If
    End Sub
    Private Sub DataGridViewPop_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewPop.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            selection()
        End If
    End Sub

    Private Sub selection()
        Try
            If Not DataGridViewPop.SelectedCells(0).Value.ToString = "" Then


                If Not DataGridViewPop.SelectedCells(4).Value.ToString = "" Then
                    Me.TextBoxLong.Text = DataGridViewPop.SelectedCells(4).Value
                Else
                    Me.TextBoxLong.Text = ""
                End If
                If Not DataGridViewPop.SelectedCells(3).Value.ToString = "" Then
                    Me.TextBoxLat.Text = DataGridViewPop.SelectedCells(3).Value
                Else
                    Me.TextBoxLat.Text = ""
                End If
                If Not DataGridViewPop.SelectedCells(5).Value.ToString = "" Then
                    Me.TextBoxAlt.Text = DataGridViewPop.SelectedCells(5).Value
                Else
                    Me.TextBoxAlt.Text = ""
                End If
                If Not DataGridViewPop.SelectedCells(2).Value.ToString = "" Then
                    Me.TextBoxLocl.Text = DataGridViewPop.SelectedCells(2).Value
                Else
                    Me.TextBoxLocl.Text = ""
                End If
                If Not DataGridViewPop.SelectedCells(6).Value.ToString = "" Then
                    Me.oidValue.Text = DataGridViewPop.SelectedCells(6).Value
                Else
                    Me.oidValue.Text = ""
                End If

            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxLong.Text = ""
        Me.TextBoxLat.Text = ""
        Me.TextBoxAlt.Text = ""
        Me.TextBoxLocl.Text = ""
    End Sub
    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Entrer = ""
            If action = "ajouter" Then
                db.myReq = "INSERT INTO  [dbo].[LOCALITESITE]([ID_Sit],[LOCNOM_POP],[LATMOY_POP],[LONMOY_POP],[ALTMOY_POP],[SIGID]) VALUES('" & db.idSite & "','" & Me.TextBoxLocl.Text & "','" & Me.TextBoxLat.Text & "','" & Me.TextBoxLong.Text & "','" & Me.TextBoxAlt.Text & "','" & Me.oidValue.Text & "')"

            Else
                db.myReq = "UPDATE  [dbo].[LOCALITESITE] SET [ID_Sit] = '" & db.idSite & "',[LOCNOM_POP] = '" & Me.TextBoxLocl.Text & "',[LATMOY_POP] = '" & Me.TextBoxLat.Text & "',[LONMOY_POP] = '" & Me.TextBoxLong.Text & "',[ALTMOY_POP] = '" & Me.TextBoxAlt.Text & "',[SIGID]='" & Me.oidValue.Text & "' WHERE [Id_Pop]='" & Me.DataGridViewPop.SelectedCells(0).Value & "'"
                MsgBox(db.myReq)
            End If
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        majour()
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[LOCALITESITE]  WHERE [Id_Pop]='" & Me.DataGridViewPop.SelectedCells(0).Value & "'"
            db.Connecter(databaseName)
            db.Disconnecter()
            majour()
            If DataGridViewPop.RowCount - 1 = 0 Then
                Return
            End If
            Dim listOid As List(Of String) = New List(Of String)

            listOid.Add(DataGridViewPop.SelectedCells(6).Value)

            _workspace.deleteNodes(_layerName, listOid)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ButtonMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMod.Click
        action = "update"
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim intStNiv As New StructInstr
        intStNiv.TextBoxIdPop.Text = Me.DataGridViewPop.SelectedCells(0).Value
        intStNiv.TextBoxPopulation.Text = Me.DataGridViewPop.SelectedCells(2).Value
        intStNiv.Visible = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim intStNiv As New StructActiv
        intStNiv.TextBoxIdPop.Text = Me.DataGridViewPop.SelectedCells(0).Value
        intStNiv.TextBoxPopulation.Text = Me.DataGridViewPop.SelectedCells(2).Value
        intStNiv.Visible = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intStNiv As New StrucuDemo
        intStNiv.TextBoxIdPop.Text = Me.DataGridViewPop.SelectedCells(0).Value
        intStNiv.TextBoxPopulation.Text = Me.DataGridViewPop.SelectedCells(2).Value
        intStNiv.Visible = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim intStNiv As New SuiviPopSit
        intStNiv.TextBoxIdPop.Text = Me.DataGridViewPop.SelectedCells(0).Value
        intStNiv.TextBoxPopulation.Text = Me.DataGridViewPop.SelectedCells(2).Value
        intStNiv.Visible = True
    End Sub



    ''' <summary>
    ''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private _workspace As Object
    Private _layerName As String
    Private geoForm As GeoLocation = Nothing
    Property layerName() As String
        Get
            Return _layerName
        End Get
        Set(ByVal value As String)
            _layerName = value
        End Set
    End Property

    Property workspace() As Object
        Get
            Return _workspace
        End Get
        Set(ByVal value As Object)
            _workspace = value
        End Set
    End Property

    Private Sub btnGetGeo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetGeo.Click
        If geoForm IsNot Nothing AndAlso geoForm.Visible Then
            Return
        End If

        geoForm = New GeoLocation()
        geoForm.Show()
        geoForm.workspace = Me.workspace
        geoForm.oidParentText = oidValue

    End Sub
End Class